<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/rp/common/taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>">
    
    <title>${ webpageTitle }</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	
	<script src="rp/scripts/boot.js" type="text/javascript"></script>
	
	<style type="text/css">
	   	
    </style>
	
  </head>
  
  <body class="body-fit">
  	
  	<div style="width:100%;">
        <div class="mini-toolbar" style="padding:0px;border-top:0;border-left:0;border-right:0;">
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" onclick="addRootNode()"><i class="fa fa-plus fa-lg"></i> 添加根部门</a>
			  			<a class="mini-button" onclick="addChildNode()"><i class="fa fa-plus fa-lg"></i> 添加子部门</a>
			  			<a class="mini-button" onclick="saveData()"><i class="fa fa-floppy-o fa-lg"></i> 保存</a>
				        <a class="mini-button mini-button-danger" onclick="removeRows()"><i class="fa fa-trash-o fa-lg"></i> 删除</a>
				        <a class="mini-button" onclick="moveOrgan()"><i class="fa fa-exchange fa-lg"></i> 移动</a>
				        <a class="mini-button" onclick="expandAllModules()"><i class="fa fa-chevron-down fa-lg"></i> 展开所有</a>
				        <a class="mini-button" onclick="collapseAllModules()"><i class="fa fa-chevron-up fa-lg"></i> 折叠所有</a>
				               
                    </td>
                    <td style="white-space:nowrap;">
                    	
                    </td>
                </tr>
            </table>           
        </div>
    </div>
    <div class="mini-fit">
	  	<div id="organTreeGrid" class="mini-treegrid" style="width: 100%; height: 100%;" borderStyle="border:0;"    
		    url="rp/system/organMng/getOrgans" showTreeIcon="true"
		    treeColumn="name" idField="id" parentField="pid" 
		    resultAsTree="false" expandOnLoad="true"
	   		allowCellEdit="true" allowCellSelect="true" multiSelect="true"
	       	editNextOnEnterKey="true" editNextRowCell="true" allowCellValid="true">
		    <div property="columns">
		        <div type="indexcolumn" headerAlign="center">序号</div>
		        <div type="checkcolumn" width="20"></div>
		        <div name="name" field="name" vtype="required" width="80" headerAlign="center">
		        	部门名称<input property="editor" class="mini-textbox" style="width:100%;" />
		        </div>
		        <div name="remark" field="remark" width="200" headerAlign="center" >
		        	备注<input property="editor" class="mini-textarea" style="width:200px;" minWidth="200" minHeight="50"/>
		        </div>
		        <div field="createDate" width="70" headerAlign="center" allowSort="true" renderer="onDateRenderer">
		        	创建日期
		        </div>                
		    </div>
		</div>
	</div>
	
	<div id="moveWindow" title="请选择目标节点" class="mini-window" style="width:250px;height:350px;"
        showModal="true" showFooter="true" allowResize="true">
        <a class="mini-button" onclick="okWindow('root')">移动至根节点</a>
        <ul id="organTree" class="mini-tree" style="width:100%;" 
            showTreeIcon="true" textField="name" idField="id" parentField="pid">        
        </ul>
        <div property="footer">
            <table style="width:100%">
                <tr>
                    <td>
                    	<a class="mini-button" onclick="okWindow()"><i class="fa fa-check fa-lg"></i> 确定</a>
                    	<a class="mini-button" onclick="hideWindow()"><i class="fa fa-times fa-lg"></i> 取消</a>
                    	
                    </td>
                </tr>            
            </table>
        </div>
    </div>
  	
	<script type="text/javascript">
		var grid;
		var moveWindow;
		var organTree;
	
		$(function(){
			mini.parse();
			grid = mini.get("organTreeGrid");
			moveWindow = mini.get("moveWindow");
			organTree = mini.get("organTree");
			
		});
		
		function addRootNode() {
            var newNode = { name: "" };
            grid.addNode(newNode, 2);
            grid.beginEditCell(newNode, "name");
        }

        function addChildNode(){
			var selectedNode = grid.getSelectedNode();
			if(!selectedNode){
				mini.alert("您未选中任何节点");
			}else{
				var newNode = { name: "" };
				grid.addNode(newNode, "add", selectedNode);
				grid.beginEditCell(newNode, "name");
			}
		}

		function saveData() {
        	grid.validate();
			if (grid.isValid() == false) {
                var error = grid.getCellErrors()[0];
                grid.beginEditCell(error.record, error.column);
                return;
            }
			var data = grid.getChanges();
            var json = rp_encode(data);
            grid.loading("保存中，请稍等......");
            rp_ajax("rp/system/organMng/saveOrgans", {json: json}, function(result){
            	if(result == "success"){
            		grid.reload();
            	}else{
            		grid.unmask();
            		mini.alert(result);
            	}
            });
        }

        function removeRows(){
	        var rows = grid.getSelecteds();
	        if(rows.length > 0){
		        mini.confirm("确定删除记录吗？", "友情提示",
   	            function (action) {
   	                if (action == "ok") {
   	                    var json = rp_encode(rows);
   	                    if (rows.length > 0) {
   	                    	rp_ajax("rp/system/organMng/deleteOrgans", {json: json}, function(result){
   	                    		//grid.removeNodes(rows);
   	                    		if(result == "success"){
	   	                     		grid.reload();
	   	                     	}else{
	   	                     		grid.unmask();
	   	                     		mini.alert(result);
	   	                     	}
   	                        });
   	                    }
   	                }
   	            }
   	        );
	        }else{
		        mini.alert("请先选择要删除的节点");
	        }
        	
        }

        function expandAllModules(){
			grid.expandAll();
		}
		
		function collapseAllModules(){
			grid.collapseAll();
		}
		
		var selectedNodes;
		function moveOrgan(){
			selectedNodes = grid.getSelectedNodes();
			if(selectedNodes.length == 0){
				mini.alert("请先选择要移动的节点");
			}else{
				treeData = mini.clone(grid.getData());
		       	organTree.loadData(treeData);
		       	moveWindow.show();
			}
		}

		function okWindow(_root){
			var ids = "";
			for(var i=0;i<selectedNodes.length;i++){
				if(i == selectedNodes.length-1){
					ids = ids + selectedNodes[i].id;
				}else{
					ids = ids + selectedNodes[i].id+",";
				}
			}
			
			if(_root){
				rp_ajax("rp/system/organMng/moveOrgansToRoot",{ids:ids},function(result){
					if(result == "success"){
						selectedNodes = null;
						hideWindow();
						grid.load();
					}else{
						mini.alert("操作失败");
					}
				});
				return;
			}
			
			var selectedNode = organTree.getSelectedNode();
			if(!selectedNode){
				mini.alert("请选择一个目标节点");
			}else{
				var targetOrganId = selectedNode.id;
				for(var i=0;i<selectedNodes.length;i++){
					if(selectedNodes[i].id == selectedNode.id){
						mini.alert("选择的目标节点不能与需要移动的节点相同");
						return;
					}
				}
				rp_ajax("rp/system/organMng/moveOrgans",{ids:ids, targetId:selectedNode.id},function(result){
					if(result == "success"){
						selectedNodes = null;
						hideWindow();
						grid.load();
					}else{
						mini.alert(result);
					}
				});
			}
		}

		function hideWindow(){
			moveWindow.hide();
		}
	</script>
  </body>
</html>
